<template>
  <div>
    <el-dialog :title="info.isAdd ?'分类添加':'分类修改'" :visible.sync="info.isshow" @close="close">
      user:{{user}}
      <el-form :model="user">
        <el-form-item label="上级分类" label-width="120px">
          <el-select v-model="user.pid">
            <el-option label="顶级分类" :value="0"></el-option>
            <el-option v-for="item in arr" :key="item.id" :label="item.catename" :value="item.id" :disabled="user.id == item.id"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="分类名称" label-width="120px">
          <el-input v-model="user.catename" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="user.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>

        <el-form-item label="图片" label-width="120px">
          <div>
            <div class="my-upload">
              <h3>+</h3>
              <img v-if="imgUrl" :src="imgUrl" alt />
              <input type="file" @change="getFile" />
            </div>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add()">添加</el-button>
        <el-button type="primary" v-else @click="update">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { IndexRoutes } from "../../../router/index";
import { successAlert, errorAlert } from "../../../utils/alert.js";
import {reqcateinfo,reqcateedit} from "../../../http/api"
import path from "path";
import {
  reqroleadd,
  reqmenuedit,
  reqmenuinfo,
  reqrolelist,
  reqmenulist,
  reqroleinfo,
  reqroleedit,
  reqcateadd
} from "../../../http/api";
import { formClose } from "../../../mixins";

export default {
  //接受info
  props: ["info"],
  mixins: [formClose],
  data() {
    return {
      //初始化表单数据
      user: {
        pid: "",
        catename: "",
        img: null,
        status: 1
      },
      imgUrl: "", //图片地址
    };
  },
  computed: {
    ...mapGetters({
      arr: "cate/arr"
    })
  },
  mounted() {},
  methods: {
    ...mapActions({
      changeUserInfo: "changeUserInfo",
      reqArr: "cate/reqArr"
    }),

    //选了图片
    getFile(e) {
      let file = e.target.files[0];

      //验证文件大小
      if (file.size > 2 * 1024 * 1024) {
        errorAlert("文件最大2M");
        return;
      }
      //验证文件类型
      let extname = path.extname(file.name);
      let imgArr = [".png", ".jpg", ".jpeg", ".gif"];
      if (!imgArr.includes(extname)) {
        errorAlert("请上传正确的格式");
        return;
      }

      //生成一张图
      this.imgUrl = URL.createObjectURL(file);
      //赋值给user.img
      this.user.img = file;
    },
    //清空user
    empty() {
      this.user = {
        pid: "",
        catename: "",
        img: null,
        status: 1
      };
      this.imgUrl = "";
    },
    //点了添加
    add() {
      reqcateadd(this.user).then(res => {
        if (res.data.code == 200) {
          //弹成功
          successAlert(res.data.msg);
          //弹框清空
          this.empty();
          //弹框消失
          this.info.isshow = false;
          //重新请求列表
          this.reqArr();
        }
      });
    },
    //上级菜单发生了改变
    changepid() {
      //如果上级菜单是顶级菜单，那么类型就是目录。否则就是菜单，看pid
      if (this.user.pid == 0) {
        this.user.type = 1;
      } else {
        this.user.type = 2;
      }
    },
    //获取一条数据
    getOne(id) {
      reqcateinfo({ id: id }).then(res => {
        if (res.data.code == 200) {
          this.user=res.data.list;
          this.user.id = id;
          this.imgUrl = this.$pre+this.user.img
        }
      });
    },
    // 修改
    update() {
      reqcateedit(this.user).then(res => {
        if (res.data.code == 200) {
          //弹成功
          successAlert(res.data.msg);
          //弹框清空
          this.empty();
          //弹框消失
          this.info.isshow = false;
          //重新请求列表
          this.reqArr()
        }
      });
    }
  }
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
.my-upload {
  width: 100px;
  height: 100px;
  border: 1px dashed @primary-light;
  position: relative;
  h3 {
    text-align: center;
    line-height: 100px;
  }
  input,
  img {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;

    cursor: pointer;
  }
  input {
    opacity: 0;
  }
}
</style>